//
//  TurnNoNetView.m
//  ebook
//
//  Created by mt on 16/8/31.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "PageErrorView.h"
#import "ReaderGlobalModel.h"
#import "ALBatteryView.h"
#import "PCReaderTool.h"

@interface PageErrorView ()<UIGestureRecognizerDelegate>

@property (strong, nonatomic) UILabel *msgLabel;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIButton *reLoadBtn;

@property (nonatomic, strong) Chapter *chapter;
@property (nonatomic, retain) UIColor *titleColor;

@end

@implementation PageErrorView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.msgLabel];
        [self addSubview:self.reLoadBtn];
        [self addSubview:self.titleLabel];
        self.reLoadBtn.centerX = self.msgLabel.centerX;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBgColor:) name:kChangePageColorNotification object:nil];
    
    return self;
}

-(void)updateBgColor:(NSNotification *)noti{
    
    _msgLabel.textColor = self.titleColor;
    [_reLoadBtn setTitleColor:self.titleColor forState:UIControlStateNormal];
    _reLoadBtn.layer.borderColor = self.titleColor.CGColor;
}

- (UILabel *)msgLabel {
    
    if (!_msgLabel) {
        _msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, MTScreenH/2-100, MTScreenW-60, 50)];
        _msgLabel.text = kContentErrorNoNetInfo;
        _msgLabel.numberOfLines = 0;
        _msgLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _msgLabel.font = [UIFont systemFontOfSize:16.f];
        _msgLabel.textAlignment = NSTextAlignmentCenter;
        _msgLabel.textColor = self.titleColor;
    }
    return _msgLabel;
}

- (UIButton *)reLoadBtn {
    
    if (!_reLoadBtn) {
        _reLoadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat width = 80;
        _reLoadBtn.frame = CGRectMake((MTScreenW-width)/2, CGRectGetMaxY(self.msgLabel.frame)+5, width, 30);
        [_reLoadBtn setTitle:@"重试" forState:UIControlStateNormal];
        [_reLoadBtn setTitleColor:self.titleColor forState:UIControlStateNormal];
        _reLoadBtn.layer.borderWidth = 1.0;
        _reLoadBtn.layer.borderColor = self.titleColor.CGColor;
        _reLoadBtn.layer.cornerRadius = 5;
        [_reLoadBtn addTarget:self action:@selector(reloadBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reLoadBtn;
}

- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.frame = CGRectMake(15, 5-1, self.width-30, 25);
        _titleLabel.textColor = [ReaderGlobalModel shareModel].readerConfig.TitleTextColor;
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textAlignment = NSTextAlignmentJustified;
    }
    return _titleLabel;
}

-(UIColor *)titleColor{
    return [ReaderGlobalModel shareModel].readerConfig.ContentTextColor;
}

- (void)setTitle:(NSString*)title{
    _titleLabel.text = title;
}

//设置章节
- (void)setChapter:(Chapter *)chapter{
    _chapter = chapter;
}

//设置错误消息
- (void)setErrorInfo:(NSString *)msg{
    _msgLabel.text = msg;
}

- (void)reloadBtnClick {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kRefreshAdnReloadChapterNotification object:_chapter];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
